#include "defaultsvggraphicsview.h"
#include <QFileInfo>
#include <QMouseEvent>
DefaultSvgGraphicsView::DefaultSvgGraphicsView(QString svgFileName,QWidget *parent)
	: QGraphicsView(parent)
{
	m_svgFileName = svgFileName;
	m_scene = new QGraphicsScene;
	m_scene->setSceneRect(this->rect());
}

DefaultSvgGraphicsView::~DefaultSvgGraphicsView()
{
}

void DefaultSvgGraphicsView::SetSvgFileName(QString fileName)
{
	m_svgFileName = fileName;
}

void DefaultSvgGraphicsView::showEvent(QShowEvent* event)
{
	Init();
}

void DefaultSvgGraphicsView::Init()
{
	m_scene = new QGraphicsScene;
	m_scene->setSceneRect(this->rect());
	this->setScene(m_scene);
	QFileInfo info(m_svgFileName);
	if (!info.exists())
	{
		return;
	}
	m_svgItem = new QGraphicsSvgItem(m_svgFileName);
	m_scene->addItem(m_svgItem);
	m_svgItem->setTransformOriginPoint(m_svgItem->boundingRect().width() / 2, m_svgItem->boundingRect().height() / 2);
}

void DefaultSvgGraphicsView::wheelEvent(QWheelEvent* event)
{
	qreal scale = m_svgItem->scale();
	QWheelEvent* wheelEvent = (QWheelEvent*)event;
	if (wheelEvent->delta() > 0)
		scale += 0.1;
	else
		scale -= 0.1;
	m_svgItem->setScale(scale);

	QGraphicsView::wheelEvent(event);
}